package cn.ujn.edu.microservice01mq.service.impl;

import cn.ujn.edu.microservice01mq.entity.User;
import cn.ujn.edu.microservice01mq.enums.UserStatus;
import cn.ujn.edu.microservice01mq.mapper.UserMapper;
import cn.ujn.edu.microservice01mq.service.IUserService;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.management.RuntimeErrorException;
import java.util.Optional;

/**
 * @Auther: shzhu
 * @Date: 2024/11/7 19:39
 * @Description:
 */
@Service
@Transactional
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    public void incrementBalance(Long id, double amount) {
        User user = this.getById(id);
        if (user == null || user.getStatus() == UserStatus.FROZEN) {
            throw new RuntimeErrorException(null, "用户不存在或用户被冻结");
        }
        lambdaUpdate()
                .set(User::getBalance, user.getBalance() + amount)
                .eq(User::getId, id)
                .update();
    }
}
